--Hooks at bottom!

AddCSLuaFile( "cl_init.lua" ) 				--Tell the server that the client needs to download cl_init.lua.
AddCSLuaFile( "shared.lua" ) 				--Tell the server that the client needs to download shared.lua.
--Client Side
AddCSLuaFile( "cside/p_hud.lua" ) 			--Holds the player hud code.
AddCSLuaFile( "cside/menu_interface.lua" ) 	--Menus for choosing teams, buy weapons, etc.
AddCSLuaFile( "cside/cconsole_cmds.lua" )   --Console commands.

--Serverside code gamemode.
include( 'shared.lua' )
include( 'sside/jobfuncs.lua' )
include( 'sside/sconsole_cmds.lua' )

function GM:PlayerInitialSpawn( ply ) 	
    ply:SetTeam( 1 ) 						--Add the player to team 1.
	ply:SetModel( jobmodeltable[1] )
end 										
 
function GM:PlayerLoadout( ply ) 			--Weapon/ammo/item function.
    if ply:Team() == 1 then 				--If player team equals 1.
 
        ply:Give( "weapon_physcannon" ) 	--Give them the Gravity Gun.
 
    end 									--Here we end the if condition.
 
end 										--Here we end the Loadout function.
 
 function GM:PlayerCanJoinTeam( ply )
    return ply:IsAdmin( )
end
 
-- Create a hook for the GM:PlayerDeath() function that
-- sets our two variables when the player dies

-- Set the maximum time in seconds before a player must respawn
local maxdeathtime = 30;

function player_initdeath( ply, wep, killer )
 
     ply.nextspawn = CurTime() + maxdeathtime; -- set when we want to spawn
 
end
 
function playerforcerespawn( ply )
 
     if (CurTime()>=ply.nextspawn) then
          ply:Spawn()
          ply.nextspawn = math.huge
     end
 
end

--Chat parser

function chatChangeJob( ply, text, team )

	if string.sub(text, 1, 4) == "/job" then
		
		for i=1, table.maxn(jobtable) do
			-- If /job and it finds a match in the job table then it assigns job. Case insensitive.
			if string.find( string.lower(text), string.lower(jobtable[i])) then
				
				MsgN( "Job changed to:"..jobtable[i])
				ply:SetTeam( i ) 	
				ply:ChatPrint("Report Sent.")
				return
				
			end	
			
		end
	
	ply:ChatPrint("Enter a playable job!")
	return
	
	end
	
end

function chatReport( ply, text, team )
	local playerToReport
	local exists
	local sTableHolder
	
	sTableHolder = string.Explode(" ", text)
	exists = false
	playerToReport = string.match(text, '[ - ]')    -- "[ ]" is used for sets in pattern matching. "-" denotes range.	
	
	 if string.sub(text, 1, 4) ==  "/report" then
		
		--Check to see if the player exists ';/';/,. vm b,ist, print an error and return!
		if exists == false then
		    
			ply:ChatPrint("Player does not exist!")
			return
			
		end
		
		for _, v in ipairs(player.GetAll()) do
		
			if v:IsUserGroup("moderator") then
			
				v:ChatPrint("Player: "..playerToReport.." is being reported by "..ply:Nick().." for"..sTableHolder[2])
				
				
			end
			
		end
		
	end
	
end

--Hooks
hook.Add( "PlayerDeath", "player_initalize_dvars", player_initdeath )
hook.Add( "PlayerDeathThink", "player_step_forcespawn", playerforcerespawn )
hook.Add( "PlayerSay", "chat_Change_Job", chatChangeJob )
hook.Add( "PlayerSay", "chat_report", chatReport )


